#06/10/2012 Tai-Hsien Ou Yang

FILE_LOCATION   =	"C:\\Users\\Student\\Desktop\\SoapFiles"
FILE_INPUT_PED 	=	"Soapv2.fam"
FILE_OUTPUT	=	"parchild.lst"
FILE_OUTPUT2	=	"gparchild.lst"
FILE_OUTPUT3 =	"siblings.lst"
FILE_OUTPUT4 = "halfsiblings.lst"


setwd(FILE_LOCATION)
dataset.ped<-scan(FILE_INPUT_PED , list(fid=0,id="",pr="",mt="", sex=0, note=0))
  
#	Take out only what we want

pairlist<-c()
pair1<-c()
pair2<-c()

    for(j in 1:(length(dataset.ped$id)))
    {
	
	pair1=cbind(dataset.ped$id[j],dataset.ped$pr[j]) 
	pair2=cbind(dataset.ped$id[j],dataset.ped$mt[j]) 
	pairlist<-rbind(pairlist,pair1)
	pairlist<-rbind(pairlist,pair2)
	
	}
	
write.table(pairlist, FILE_OUTPUT, row.names=F,col.names=F,append=F)



#drewdanohurhurhur
#6/10/2012

poplist<-unique(c(dataset.ped$id,dataset.ped$pr,dataset.ped$mt))
bob<-integer(0)

for (i in seq(length(poplist)))
	{
	if (grepl("-",poplist[i])) bob<-c(bob,-i)
	else if (!(grepl("K",poplist[i]))) bob<-c(bob,-i)
	}
poplist<-poplist[bob]




gpairlist<-c(0,0)
for (i in seq(length(poplist)))
{
temp1<-dataset.ped$pr[which(dataset.ped$id==poplist[i])]
temp2<-dataset.ped$mt[which(dataset.ped$id==poplist[i])]
g1<-dataset.ped$pr[which(dataset.ped$id==temp1)]
g2<-dataset.ped$mt[which(dataset.ped$id==temp1)]
g3<-dataset.ped$pr[which(dataset.ped$id==temp2)]
g4<-dataset.ped$mt[which(dataset.ped$id==temp2)]
gpairlist<-rbind(gpairlist,c(poplist[i],g1))
gpairlist<-rbind(gpairlist,c(poplist[i],g2))
gpairlist<-rbind(gpairlist,c(poplist[i],g3))
gpairlist<-rbind(gpairlist,c(poplist[i],g4))
}

tom<-integer(0)
for (i in seq(length(gpairlist[,1])))
	{
	if (grepl("-",gpairlist[i,1]) | grepl("-",gpairlist[i,2])) tom<-c(tom,-i)
	else if (!(grepl("K",gpairlist[i,1]) & grepl("K",gpairlist[i,2]))) tom<-c(tom,-i)
	}
gpairlist2<-gpairlist[tom,]

tom<-integer(0)
for (i in seq(length(gpairlist2[,1])))
	{
	if (gpairlist2[i,1]==gpairlist2[i,2]) tom<-c(tom,-i)
	}
gpairlist2<-gpairlist2[tom,]

gpairlist<-gpairlist2

write.table(gpairlist, FILE_OUTPUT2, row.names=F,col.names=F,append=F)



############################write the code for sib lists############################
siblist<-c(0,0)
for (i in seq(length(dataset.ped$id)))
{
	temp1<-dataset.ped$pr[i]
	temp2<-dataset.ped$mt[i]
	if (!(grepl("-",temp1)) & (grepl("K",temp1)))
	{
	if (!(grepl("-",temp2)) & (grepl("K",temp2)))
	{
	for (j in (i+1):length(dataset.ped$id))
	{
		if (j<length(dataset.ped$pr)){
		if((dataset.ped$pr[j] == temp1) & (dataset.ped$mt[j] == temp2))
		{siblist<-rbind(siblist,c(dataset.ped$id[j],dataset.ped$id[i]))}}
	}
	}
	}
}

tom<-integer(0)
for (i in seq(length(siblist[,1])))
	{
	if (grepl("-",siblist[i,1]) | grepl("-",siblist[i,2])) tom<-c(tom,-i)
	else if (!(grepl("K",siblist[i,1]) & grepl("K",siblist[i,2]))) tom<-c(tom,-i)
	}
siblist<-siblist[tom,]

write.table(siblist, FILE_OUTPUT3, row.names=F,col.names=F,append=F)





############################write the code for half sib lists################################
halfsiblist<-c(0,0)
for (i in seq(length(dataset.ped$id)))
{
	temp1<-dataset.ped$pr[i]
	temp2<-dataset.ped$mt[i]
	for (j in (i+1):length(dataset.ped$id))
	{
		if (j<length(dataset.ped$pr))
		{
		if((dataset.ped$pr[j] == temp1 & dataset.ped$mt[j] != temp2) | (dataset.ped$pr[j] != temp1 & dataset.ped$mt[j] == temp2))
		{
		if((dataset.ped$pr[j] == temp1 & dataset.ped$mt[j] != temp2) & (!(grepl("-",temp1)) & grepl("K",temp1)))
		{halfsiblist<-rbind(halfsiblist,c(dataset.ped$id[j],dataset.ped$id[i]))}
		if((dataset.ped$pr[j] == temp2 & dataset.ped$mt[j] != temp1) & (!(grepl("-",temp2)) & grepl("K",temp2)))
		{halfsiblist<-rbind(halfsiblist,c(dataset.ped$id[j],dataset.ped$id[i]))}
		}
		}
	}
}

tom<-integer(0)
for (i in seq(length(halfsiblist[,1])))
	{
	if (grepl("-",halfsiblist[i,1]) | grepl("-",halfsiblist[i,2])) tom<-c(tom,-i)
	else if (!(grepl("K",halfsiblist[i,1]) & grepl("K",halfsiblist[i,2]))) tom<-c(tom,-i)
	}
halfsiblist<-halfsiblist[tom,]

write.table(halfsiblist, FILE_OUTPUT4, row.names=F,col.names=F,append=F)
################################################################################################################

